package sk.tramtatiri.pages.users;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.border.Border;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

public class CollapsibleBorder extends Border {
	   public CollapsibleBorder(String id,
	     IModel<String> collapse,IModel<String> expand) {
	      super(id, new Model<Boolean>());
	      setCollapsed(true);
	      WebMarkupContainer body = new WebMarkupContainer("body")
	 {
	         protected void onConfigure() {
	            setVisible(!isCollapsed());
	         }
	      };
	      add(body);
	      body.add(getBodyContainer());
	      Link toggle = new Link("toggle") {
	         public void onClick() {
	            setCollapsed(!isCollapsed());
	         }
	      };
	      add(toggle);
	      toggle.add(new Label("caption",
	        new ExpandCollapseModel(collapse,expand)));
	   }
	   public void setCollapsed(boolean collapsed) {
	      setDefaultModelObject(collapsed);
	   }
	   public boolean isCollapsed() {
	      return Boolean.TRUE.equals(getDefaultModelObject());
	   }
	   private class ExpandCollapseModel extends 
	     AbstractReadOnlyModel<String> {
	      private final IModel<String> collapse;
	      private final IModel<String> expand;
	      public ExpandCollapseModel(IModel<String> collapse,
	            IModel<String> expand) {
	         this.collapse = collapse;
	         this.expand = expand;
	      }
	      public String getObject() {
	         if (isCollapsed()) {
	            return expand.getObject();
	         } else {
	            return collapse.getObject();
	         }
	      }
	      public void detach() {
	         collapse.detach();
	         expand.detach();
	      }
	   }
	}